<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小西瓜 <751707632@qq.com>
// +----------------------------------------------------------------------
namespace app\fowl\controller;

use cmf\controller\AdminBaseController;
use think\Validate;
use think\Db;
use think\Session;
use app\fowl\model\ImsRegionsModel;
use app\portal\model\PortalCategoryModel;
use app\fowl\model\ImsStramingLiveModel;
use app\fowl\model\ImsStramingRestrictedModel;
use app\fowl\model\ImsStramingGameModel;

class AdminStramingController extends AdminBaseController
{
    /**
     * 直播列表
     */
    public function stramingList()
    {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别
        $search = array();
        // 查询条件
        $param = $this->request->param();
        if ( request()->isPost() ) {
            if ( !empty($param['start_time']) && !empty($param['end_time']) ) {
                $where['vm.start_time'] = ['between',[$param['start_time'],$param['end_time']]]; // 时间区间
            }
            if ( !empty($param['keys']) && !empty($param['keyword']) ) {
                if ( $param['keys'] == 'user_id' ) {
                    $user_id = Db::name('ims_member')->where('staff_name', $param['keyword'])->value('id');
                    $where['vm.user_id'] = $user_id;
                } elseif ( $param['keys'] == 'category' ) {
                    $category_id = Db::name('portal_category')->where('name', $param['keyword'])->value('id');
                    $where['vm.type_id'] = $category_id;
                } elseif ( $param['keys'] == 'challenge' ) {
                    $where['vm.challenge_name'] = ['like', '%'.$param['keyword'].'%'];
                } else {
                    $where['vm.vm_title'] =  ['like', '%'.$param['keyword'].'%'];
                }
            }
            if ( isset($param['mStatus']) && $param['mStatus'] != '' ) {
                $where['vm.status'] = $param['mStatus'];
            }
            isset($param['start_time']) ? $search['start_time'] = $param['start_time'] : '';
            isset($param['end_time']) ? $search['end_time'] = $param['end_time'] : '';
            isset($param['keys']) ? $search['keys'] = $param['keys'] : '';
            isset($param['keyword']) ? $search['keyword'] = $param['keyword'] : '';
            isset($param['mStatus']) ? $search['mStatus'] = $param['mStatus'] : '';
            Session::set('stramingList',$where);
            Session::set('stramingListKeyword',$search);
        } else {
            if ( isset($param['page']) ) {
                $where = Session::get('stramingList');
                $search = Session::get('stramingListKeyword'); 
            } else {
                Session::set('stramingList', null);
                Session::set('stramingListKeyword',null);
            }
        }

        // 查询列表
        $where['vm.is_delete'] = 0;
        $stramingModel = new ImsStramingLiveModel();
        $stramingList = $stramingModel->stramingList($where, 'id desc', 8);

        $this->assign([
            'stramingList'       =>  $stramingList,
            'search'            => $search,
            'starmingStatus'     => stramingStatus(),
        ]);
        return $this->fetch();
    }

    /**
     * 查看直播详情
     */
    public function stramingDetails()
    {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别
        $param = $this->request->param();  
        if ( empty($param['id']) ) {
            $this->error('缺少参数', 'AdminStraming/stramingList');exit;
        }

        // 获取直播信息
        $stramingModel = new ImsStramingLiveModel();
        $stramingInfo = $stramingModel->stramingInfo(['vm.id'=>$param['id']]);

        $this->assign([
            'data' => $stramingInfo,
            'stramingStatus'     => stramingStatus(),
            'stramingLabel'     => stramingLabel(),
        ]);
        return $this->fetch(); 
    }

    /**
     * 查看限制人员
     */
    public function controlLook()
    {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别
        $get = $this->request->param();
        if ( $get['vm_id'] ) {
            $where['sr.vm_id'] = $get['vm_id'];
        } else {
            $this->error('缺少参数');
        }

        // 获取参会人员列表
        $controlModel = new ImsStramingRestrictedModel();
        $controlList = $controlModel->controlList($where);

        $this->assign([
            'controlList' => $controlList,
        ]);
        return $this->fetch();
    }

    /**
     * 直播回放
     */
    public function liveLook()
    {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别
        $get = $this->request->param();
        if ( !$get['vm_id'] ) {
            $this->error('缺少参数');
        }

        $data = Db::name('ims_straming_live')->where('id', $get['vm_id'])->field('id, vm_title, cover_pic, store_link')->find();

        $this->assign([
            'data' => $data,
        ]);
        return $this->fetch();
    }

    /**
     * 设置标签
     */
    public function stramingLabel()
    {
         if ( empty($_POST['id']) ) {
            $this->error('缺少参数');
        } else {
            $data['labels'] = $_POST['msg'];
            $data['update_time'] = date('Y-m-d H:i:s', time());
            if ( $res = Db::name('ims_straming_live')->where('id', 'in', $_POST['id'])->update($data) ) {
                // $this->traceLog('培训设置标签-成功', $_POST); // 操作日志
                $this->success('标签设置成功');
            } else {
                // $this->traceLog('培训设置标签-失败', $_POST); // 操作日志
                $this->error('标签设置失败');
            }
        }
    }

    /**
     * 查看PK资讯
     */
    public function gameLook()
    {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别
        $param = request()->param();
        if ( empty($param['vm_id']) ) {
            $this->error('缺少参数');
        }

        // 查询PKID
        $where['sj.id'] = Db::name('ims_straming_game')->where('initiator_vm_id', $param['vm_id'])->whereor('party_vm_id', $param['vm_id'])->value('id');

        if ( $where['sj.id'] ) {
            // 查询PK资讯
            $gameModel = new ImsStramingGameModel();
            $gameInfo = $gameModel->gameInfo($where);
            $this->assign([
                'initiator' => $gameInfo['initiator'],
                'partys' => $gameInfo['party'],
                'stramingStatus'   => stramingStatus(),
            ]);
        }

        return $this->fetch();
    }

    /**
     * 直播公开或隐藏
     */
    public function stramingOpen()
    {
         if ( empty($_POST['id']) ) {
            $this->error('缺少参数');
        } else {
            $data['update_time'] = date('Y-m-d H:i:s', time());
            if ( $_POST['msg'] == 1 ) {
                $data['is_open'] = 1;
                if ( $res = Db::name('ims_straming_live')->where('id', 'in', $_POST['id'])->where('is_open', 0)->where('status', 'in', [2,4,5])->update($data) ) {
                    // $this->traceLog('公开直播-success', $_POST); // 操作日志
                    $this->success('公开了'.$res.'条数据');
                } else {
                    // $this->traceLog('公开直播-error', $_POST); // 操作日志
                    $this->error('公开失败');
                }
            } else {
                $data['is_open'] = 0;
                if ( $res = Db::name('ims_straming_live')->where('id', 'in', $_POST['id'])->where('is_open', 1)->where('status', 'in', [2,4,5])->update($data) ) {
                    // $this->traceLog('隐藏直播-success', $_POST); // 操作日志
                    $this->success('隐藏'.$res.'条数据');
                } else {
                    // $this->traceLog('隐藏直播-error', $_POST); // 操作日志
                    $this->error('隐藏失败');
                }
            }
        }
    }

    /**
     * 直播删除
     */
    public function stramingDelete()
    {
        if ( empty($_POST['id']) ) {
            $this->error('缺少参数');
        } else {
            $data['is_delete'] = 1;
            $data['delete_time'] = date('Y-m-d H:i:s', time());
            if ( $res = Db::name('ims_straming_live')->where('id', 'in', $_POST['id'])->where('is_delete', 0)->update($data) ) {
                // $this->traceLog('删除直播-SUCCESS', $_POST); // 操作日志
                $this->success('成功删除'.$res.'条数据');
            } else {
                // $this->traceLog('删除直播-error', $_POST); // 操作日志
                $this->error('删除失败');
            }
        }
    }

    /****************************************************/

    /**
     * 直播审核列表
     */
    public function stramingAuditList()
    {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别
        $search = array();
        // 查询条件
        $param = $this->request->param();
        if ( request()->isPost() ) {
            if ( !empty($param['start_time']) && !empty($param['end_time']) ) {
                $where['vm.start_time'] = ['between',[$param['start_time'],$param['end_time']]]; // 时间区间
            }
            if ( !empty($param['keys']) && !empty($param['keyword']) ) {
                if ( $param['keys'] == 'user_id' ) {
                    $user_id = Db::name('ims_member')->where('staff_name', $param['keyword'])->value('id');
                    $where['vm.user_id'] = $user_id;
                } elseif ( $param['keys'] == 'category' ) {
                    $category_id = Db::name('portal_category')->where('name', $param['keyword'])->value('id');
                    $where['vm.type_id'] = $category_id;
                } elseif ( $param['keys'] == 'challenge' ) {
                    $where['vm.challenge_name'] = ['like', '%'.$param['keyword'].'%'];
                } else {
                    $where['vm.vm_title'] =  ['like', '%'.$param['keyword'].'%'];
                }
            }
            isset($param['start_time']) ? $search['start_time'] = $param['start_time'] : '';
            isset($param['end_time']) ? $search['end_time'] = $param['end_time'] : '';
            isset($param['keys']) ? $search['keys'] = $param['keys'] : '';
            isset($param['keyword']) ? $search['keyword'] = $param['keyword'] : '';
            Session::set('stramingList',$where);
            Session::set('stramingListKeyword',$search);
        } else {
            if ( isset($param['page']) ) {
                $where = Session::get('stramingList');
                $search = Session::get('stramingListKeyword'); 
            } else {
                Session::set('stramingList', null);
                Session::set('stramingListKeyword',null);
            }
        }

        // 查询列表
        $where['vm.is_delete'] = 0;
        $where['vm.status'] = 1;
        $stramingModel = new ImsStramingLiveModel();
        $stramingList = $stramingModel->stramingList($where, 'id desc', 8);

        $this->assign([
            'stramingList'       =>  $stramingList,
            'search'            => $search,
            'starmingStatus'     => stramingStatus(),
        ]);
        return $this->fetch();
    }

    /**
     * 直播审核
     */
    public function stramingAudit()
    {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别
        $param = $this->request->param();
        if ( $this->request->isAjax() ) {
            if ( !isset($param['id']) || !isset($param['status']) || empty($param['id']) || empty($param['status']) ) {
                $this->error('缺少参数');exit;
            }

            $data['status'] = $param['status'];
            $data['update_time'] = date('Y-m-d H:i:s', time());
            if ( $res = Db::name('ims_straming_live')->where('id', 'in', $param['id'])->where('status', 1)->update($data) ) {
                // $this->traceLog('直播审核-SUCCESS', $param); // 操作日志
                $this->success('审核完成', 'AdminStraming/stramingAuditList');
            } else {
                // $this->traceLog('直播审核-error', $param); // 操作日志
                $this->error('审核失败');
            }
        } else {
            if ( empty($param['id']) ) {
                $this->error('缺少参数', 'AdminStraming/stramingAuditList');exit;
            }

            // 获取直播信息
            $stramingModel = new ImsStramingLiveModel();
            $stramingInfo = $stramingModel->stramingInfo(['vm.id'=>$param['id']]);

            $this->assign([
                'data' => $stramingInfo,
            ]);
            return $this->fetch();
        }
    }

    /****************************************************/

    /**
     * [stramingRecycle 直播回收站]
     */
    public function stramingRecycle()
     {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别
        $search = array();
        // 查询条件
        $param = $this->request->param();
        if ( request()->isPost() ) {
            if ( !empty($param['start_time']) && !empty($param['end_time']) ) {
                $where['vm.start_time'] = ['between',[$param['start_time'],$param['end_time']]]; // 时间区间
            }
            if ( !empty($param['keys']) && !empty($param['keyword']) ) {
                if ( $param['keys'] == 'user_id' ) {
                    $user_id = Db::name('ims_member')->where('staff_name', $param['keyword'])->value('id');
                    $where['vm.user_id'] = $user_id;
                } elseif ( $param['keys'] == 'category' ) {
                    $category_id = Db::name('portal_category')->where('name', $param['keyword'])->value('id');
                    $where['vm.type_id'] = $category_id;
                } elseif ( $param['keys'] == 'challenge' ) {
                    $where['vm.challenge_name'] = ['like', '%'.$param['keyword'].'%'];
                } else {
                    $where['vm.vm_title'] =  ['like', '%'.$param['keyword'].'%'];
                }
            }
            if ( isset($param['mStatus']) && $param['mStatus'] != '' ) {
                $where['vm.status'] = $param['mStatus'];
            }
            isset($param['start_time']) ? $search['start_time'] = $param['start_time'] : '';
            isset($param['end_time']) ? $search['end_time'] = $param['end_time'] : '';
            isset($param['keys']) ? $search['keys'] = $param['keys'] : '';
            isset($param['keyword']) ? $search['keyword'] = $param['keyword'] : '';
            isset($param['mStatus']) ? $search['mStatus'] = $param['mStatus'] : '';
            Session::set('stramingList',$where);
            Session::set('stramingListKeyword',$search);
        } else {
            if ( isset($param['page']) ) {
                $where = Session::get('stramingList');
                $search = Session::get('stramingListKeyword'); 
            } else {
                Session::set('stramingList', null);
                Session::set('stramingListKeyword',null);
            }
        }

        // 查询列表
        $where['vm.is_delete'] = 1;
        $stramingModel = new ImsStramingLiveModel();
        $stramingList = $stramingModel->stramingList($where, 'id desc', 8);

        $this->assign([
            'stramingList'       =>  $stramingList,
            'search'            => $search,
            'starmingStatus'     => stramingStatus(),
        ]);
        return $this->fetch();
    }

    /**
     * [stramingRestore 直播还原]
     * @return [type] [description]
     */
    public function stramingRestore()
    {
        $param = request()->param();
        if ( empty($param['id']) ) {
            $this->error('缺少参数');
        } else {
            $data['is_delete'] = 0;
            $data['update_time'] = date('Y-m-d H:i:s', time());
            if ( $res = Db::name('ims_straming_live')->where('id', 'in', $param['id'])->where('is_delete', 1)->update($data) ) {
                // $this->traceLog('会议还原-SUCCESS', $param); // 操作日志
                $this->success('成功还原'.$res.'条数据');
            } else {
                // $this->traceLog('会议还原-error', $param); // 操作日志
                $this->error('还原失败');
            }
        }
    }

    /**
     * [stramingDeleteDespair 直播彻底删除]
     */
    public function stramingDeleteDespair()
    {
        $param = request()->param();
        if ( empty($param['id']) ) {
            $this->error('缺少参数');
        } else {
            $list = Db::name('ims_straming_live')->where('id', 'in', $param['id'])->where('is_delete', 1)->field('id, cover_pic')->select()->toarray();
            foreach ( $list as $key => $val ) {
                if ( $val['cover_pic'] ) $this->delFile('./uploads/', '.'.$val['cover_pic']); // 删除图片
                Db::name('ims_straming_restricted')->where('vm_id', $val['id'])->delete(); // 删除限制人员表
            }
            if ( $res = Db::name('ims_straming_live')->where('id', 'in', $param['id'])->where('is_delete', 1)->delete() ) {
                // $this->traceLog('直播彻底删除-SUCCESS', $param); // 操作日志
                $this->success('成功删除了'.$res.'条数据');
            } else {
                // $this->traceLog('直播彻底删除-error', $param); // 操作日志
                $this->error('删除失败');
            }
        }
    }
}
